const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId:0,
    searchInput:"",
    list:[],
    menuMoreStatus:false,
    menuId:0,        //分区ID
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    page: 1,          // 当前页码
    last_page:1,       //最后页码
    //位置弹窗
    positionShowStatus:false, 
    positionList:[], 
    positionArea:'全部位置',
    // 社团性质弹窗
    typeShowStatus:false,    //社团性质
    typeList:[],
    typeValue:'全部社团',
    //状态
    stateShowStatus:false,   
    stateValue:'全部状态',
    stateList:['全部状态','已认证','独立版本'],
    topHeight:0,//头部高度
    isShow:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      // isLogin: App.checkIsLogin(),
      page:1,// 当前页码
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });
    // 设置文章列表高度
    that.setListHeight();
    // topheight
    that.getTopHeight()
    // 获取服务队列表
    that.getDataList();
   
    this.setData({
      userId:App.getUserId()//判断用户ID是否存在
    })
    console.log(this.data.userId);
    //如果已经登录过了，拿下用户详细信息
    if(this.data.userId){
        this.getUserInfo()
    }else{
      that.setData({
        isShow:true
      })
    }
  },
  getTopHeight(){
    const { statusBarHeight } = wx.getSystemInfoSync();
    console.log('statusBarHeight',statusBarHeight)
    const menuButtonObject = wx.getMenuButtonBoundingClientRect();
    const { top, height } = menuButtonObject;
    console.log(top,height)
    // 此高度基于右上角菜单在导航栏位置垂直居中计算得到
    const navBarHeight = height + (top - statusBarHeight) * 2;
    console.log('navBarHeight',navBarHeight)
    this.setData({
      topHeight:statusBarHeight+navBarHeight-40,
    })
  },
 //获取用户信息
 getUserInfo(){
   console.log('getUserInfo');
  let that = this;
  App._get('user/getUserInfo', {}, result => {
  if(result.code==1){
    console.log(result.data.userInfo.shanghui_id)
    // 如果已经加入商会直接跳转到首页
    if(result.data.userInfo.shanghui_id){
      wx.switchTab({
        url: "/pages/index_sh/index"
      })
    }else{
      that.setData({
        isShow:true
      })
      console.log(result.data.userInfo)
      let shanghui = result.data.userInfo.shanghui // 创建商会申请
      let member = result.data.userInfo.ruhui      // 加入商会申请
      if(shanghui){
        that.setData({
          shanghui,
          ruHui: 2,
        })
      }
      if(member){
        that.setData({
            member,
            ruHui: 1,
        })
      }
        
    }
  }
  });
 },
  //获取数据列表
  getDataList(isPage, page){
    let obj ={
      page: page || 1,
      searchInput:this.data.searchInput,
      area:'',
      state:'',
      type:'',
    };
    if(this.data.stateValue !="全部状态"){
      obj.state = this.data.stateValue;
    }
    if(this.data.positionArea !="全部位置"){
      obj.area = this.data.positionArea;
    }
    if(this.data.typeValue !="全部社团"){
      obj.type = this.data.typeValue;
    }



    let _this = this;
    App._get('apply/shanghui',obj , function(result) {
      let resList = result.data.list,
      dataList = _this.data.list;
      if (isPage == true) {
        _this.setData({
          list: dataList.concat(resList.data),
          isLoading: false,
          last_page: resList.last_page,
        });
      } else {
        _this.setData({
          list: resList.data,
          isLoading: false,
          last_page: resList.last_page,
        });
      }
    });
  },
  //menu
  onChangMenu(e){
    console.log(e.currentTarget.dataset.id)
    this.setData({
      menuId: e.currentTarget.dataset.id,
      searchInput:"",
      menuMoreStatus: false,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
      page: 1,          // 当前页码
      last_page:1,       //最后页码
    }); 
    console.log(this.data.menuId)
    this.getDataList();
  },
  // 搜索查询
  onChangeSearch(e){
    let that = this;
    that.setData({
      searchInput: e.detail.value.searchInput,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
      page: 1,          // 当前页码
      last_page:1,       //最后页码
    }); 
    that.getDataList();
  },
  toSearch(e){
    let that = this;
    that.setData({
      searchInput: e.detail.value,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
      page: 1,          // 当前页码
      last_page:1,       //最后页码
    }); 
    that.getDataList();
  },
  // 详情页跳转
  newsDetail(e){
    let that =this;
    that.setData({
      isLogin: App.checkIsLogin(),
    });
    if(this.data.isLogin){
     let auth = e.currentTarget.dataset.auth;
     let auth_path = e.currentTarget.dataset.path;
      let nid = e.currentTarget.dataset.id;
      let name = e.currentTarget.dataset.name;
      if(auth == 2){
        // 跳转到小程序
        let aar = auth_path.split("|");
        wx.navigateToMiniProgram({
          appId: aar[0],
          path: aar[1],
          //develop开发版；trial体验版；release正式版
          envVersion: 'release', 
          success(res) {
            // 打开成功
            console.log("跳转小程序成功！",res);
          } 
        })
      }else{
        wx.navigateTo({
          url: '/pages/apply/index?shanghui_id=' + nid +'&name='+name,
        })
      }
      
    }
   
  },
  navto(e){
    var video_id =11;
    var video_name ='商会banner视频';
    wx.navigateTo({
      url: '/pages/video/detail/index?video_id='+video_id+"&video_name="+video_name,
    })
  },
  // 导航更多开关
  menuMore(e){
    console.log("menuMore",this.data.menuMoreStatus);
    let that = this;
    that.setData({
      menuMoreStatus: !that.data.menuMoreStatus
    }); 
  },
  
  /**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    console.log("翻页")
    // 已经是最后一页
    if (this.data.page >= this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },

  /**
   * 设置文章列表高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 198), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
    console.log(
      "高度 =",systemInfo.windowHeight
    );
    this.setData({
      scrollHeight
    });
  },
  /**
  * 分享当前页面
  */
  onShareAppMessage() {
    return {
      title: "选择商会",
      path: "pages/index/index"
    };
  },
  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: "选择商会",
      path: "pages/index/index"
    };
  },
// ------------------------------------------------------------------
// 搜索选择
checkSearch(e){
  let type = e.currentTarget.dataset.type;
  console.log(type,e.currentTarget.dataset.id)
  switch(type){
    case "stateShowStatus": //状态
      this.setData({
        stateValue: e.currentTarget.dataset.id,
      }); 
    break;
    case "typeShowStatus": //社团性质
      this.setData({
        typeValue: e.currentTarget.dataset.id,
      }); 
    break;
     case "positionShowStatus": //位置
      this.setData({
        positionArea: e.currentTarget.dataset.id,
      }); 
    break;
  }
  this.getDataList();
  this.cancel();
},
//显示搜索（排序、位置-商圈、社团性质、人数）
searchShow(e){
  let type = e.currentTarget.dataset.type;
  let status = !this.data[type];
  let that = this;
  if(!status){
    that.cancel();
  }
  switch(type){
    case "positionShowStatus"://位置弹窗
        App._get('apply/identity', {}, result => {
          that.setData({
            positionList:result.data.area,
            positionShowStatus:status,
            typeShowStatus:false,
            stateShowStatus:false,
          });
        }); 
    break;
    case "typeShowStatus"://社团性质
      App._get('apply/identity', {}, result => {
        that.setData({
          typeList:result.data.type,
          positionShowStatus:false,
          typeShowStatus:status,
          stateShowStatus:false,
        });
      }); 
    break;
    case "stateShowStatus"://状态
      that.setData({
        positionShowStatus:false,
        typeShowStatus:false,
        stateShowStatus:status,
      });
    break;
  }
  
},
//取消弹窗通用
cancel(){
  this.setData({
    positionShowStatus:false, //位置弹窗
    typeShowStatus:false,    //社团性质
    stateShowStatus:false,   //人数
  })
},
nate(){
  let that =this;
  that.setData({
    isLogin: App.checkIsLogin(),
  });
  if(this.data.isLogin){
    wx.navigateTo({
      url: '/pages/shanghui/add/index?type=2',
    })
  }
}
})